#include <iostream>
#include <sstream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>



int main()
{

    std::cout << "hello json" << std::endl;

    // 一、序列化
    const char* name = "小明";
    int age = 18;
    float score[] = {78.5,88,93.6};

    //1、将其保存在Json Value中
    Json::Value root;

    root["姓名"] = name;
    root["年龄"] = age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);

    //2.序列化

    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

    std::stringstream ss;

    sw->write(root,&ss);

    //3.打印看看

    std::cout << ss.str() << std::endl;
    //

    std::cout << R"("hello json")" << std::endl;

    // 二、返序列化
    std::string str = R"(  {"姓名":"小黑","年龄":18,"成绩":[66,96,54.5] }   )";
    std::string error;

    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());

    bool ret = cr->parse(str.c_str(),str.c_str()+str.size(),&root,&error);
    
    std::cout << ret << std::endl;
    std::cout << root["姓名"].asString() << std::endl;
    std::cout << root["年龄"].asInt() << std::endl;
    int sz = root["成绩"].size();
    for(int i = 0;i<sz;i++)
    {
       std::cout << root["成绩"][i] << std::endl;
    }
    return 0;
}